package com.zlzxm.shoesmarket.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.adapter.ADHomeThing;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityHomeThing;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMHomeSort;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityHomeFilter;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/7/21
 */
public class FMHomeSort extends BaseFragment implements TabLayout.OnTabSelectedListener, OnItemClickListener ,View.OnClickListener{

    private static final String TAG = "FMHomeSort";

    private View rootView;

    private RecyclerView recyclerView;

    private final ADHomeThing adHomeThing = new ADHomeThing(null);;

    private  View footView;

    private  TabLayout tabLayout;

    private  VMHomeSort vmHomeSort;

    private TextView tvFilter;


    private Observer<List<EntityHomeThing>> observerThings = entityHomeThings -> {
        Log.e(TAG,"onChanged");
        adHomeThing.setList(entityHomeThings);
    };


    private Observer<List<EntityHomeFilter>> observerFilter = this::showFilterWindow;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(rootView == null){

            rootView =  inflater.inflate(R.layout.fragment_home_sort,container,false);

            initView(rootView,savedInstanceState);
        }


        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        initViewModel();

        initData();
    }

    @Override
    protected void initView(@NonNull View view, @NonNull Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);

        footView = LayoutInflater.from(getContext())
                .inflate(R.layout.layout_list_foot,null);

        tabLayout = ZViewHelp.findById(view,R.id.tabLayout);
        tvFilter = ZViewHelp.setOnClickListener(view,R.id.tvFilter,this);

        tabLayout.addTab(tabLayout.newTab().setText("日"));
        tabLayout.addTab(tabLayout.newTab().setText("周"));
        tabLayout.addTab(tabLayout.newTab().setText("月"));

        tabLayout.addOnTabSelectedListener(this);

        recyclerView = ZViewHelp.findById(view,R.id.list);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        adHomeThing.setFooterView(footView);

        recyclerView.setAdapter(adHomeThing);

        adHomeThing.setOnItemClickListener(this);

    }


    @SuppressLint("FragmentLiveDataObserve")
    @Override
    protected void initViewModel() {

        vmHomeSort = new ViewModelProvider(this)
                .get(VMHomeSort.class);

        vmHomeSort.getLiveData().observe(this, observerThings);
        vmHomeSort.getLiveFilter().observe(this, observerFilter);

    }


    @Override
    protected void initData() {
        super.initData();

        Log.e(TAG,"adHomeThing hashcode" + adHomeThing.hashCode());
        if(vmHomeSort.isFirstLoad()) {

            sortByDay();
        }

    }



    //<editor-fold defaultState="collapsed" desc="实现">


    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        if(tab.getPosition() == 0){

            sortByDay();

        }else if(tab.getPosition() == 1){

            sortByWeek();

        }else if(tab.getPosition() == 2){

            sortByMonth();
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        ActivityIndex.startAcBillboardDetail(getActivity(),view.findViewById(R.id.ivFace),adHomeThing.getData().get(position).getUrl());
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvFilter){

            getFilterData();

        }

    }

    // </editor-fold>


    //<editor-fold defaultState="collapsed" desc="私有方法">

    private void sortByDay(){

        vmHomeSort.getData(VMHomeSort.SORT_DAY);

    }


    private void sortByWeek(){

        vmHomeSort.getData(VMHomeSort.SORT_WEEK);

    }

    private void sortByMonth(){

        vmHomeSort.getData(VMHomeSort.SORT_MONTH);

    }



    private void getFilterData(){

        List<EntityHomeFilter> list =  vmHomeSort.getLiveFilter().getValue();

        if(list==null) {

            vmHomeSort.requestFilter();

        }else {

            showFilterWindow(list);
        }

    }


    private void showFilterWindow(List<EntityHomeFilter> list){

        if(list == null || list.size()<1){

            return;
        }

        List<EntityOption> options = new ArrayList<>();

        for (EntityHomeFilter entityHomeFilter : list) {

            EntityOption entityOption = new EntityOption(entityHomeFilter.getId(),entityHomeFilter.getTitle());

            options.add(entityOption);
        }



        WindowWheelPick.getInstance()
                .setOptions(options)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {

                        infoToast(entityOption.getName());
                    }
                }).show(getChildFragmentManager(),"");


    }




    // </editor-fold>



}
